import { createRouter, createWebHistory } from 'vue-router';
import Manager from "@/views/Manager.vue";
import User from "@/views/User.vue";
import Login from "@/views/Login.vue";
import Category from "@/views/Category.vue";
import Medicine from "@/views/Medicine.vue";
import Income from "@/views/Income.vue";
import AddShop from "@/views/AddShop.vue";
import ShopCar from "@/views/ShopCar.vue";
import OrderManage from "@/views/OrderManage.vue";
import Error404 from "@/views/Error404.vue";
import HomeView from "@/views/HomeView.vue";
import TcmMarket from "@/views/TcmMarket.vue";
import { ElMessage } from 'element-plus';
import Operation from "@/views/Operation.vue";
import Person from "@/views/User/Person.vue";
import Password from "@/views/User/Password.vue";
import Chat from "@/views/merchant/Chat.vue";
import Checkout from "@/views/Checkout.vue";
import Apply from "@/views/User/Apply.vue";
import DingDan from "@/views/DingDan.vue";
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login'  // 根路径重定向到登录页
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
    },
    {
      path: '/tcmmarket',
      name: 'tcmmarket',
      meta: {
        name: '中药材商城',
        roles: ['customer'] // 只有客户可以访问
      },
      component: TcmMarket,
    },
    {
      path: '/DingDan',
      name: 'DingDan',
      meta: {
        name: '订单详情',
        roles: ['customer'] // 只有客户可以访问
      },
      component: DingDan,
    },
    {
      path: '/checkout',
      name: 'checkout',
      meta: {
        name: '结算页面',
        roles: ['customer'] // 只有客户可以访问
      },
      component: Checkout,
    },
    {
      path: '/manager',
      component: Manager,
      children: [
        {
          path: 'home',
          name: 'home',
          meta: {
            name: '首页',
            roles: ['admin', 'merchant','customer'] // 允许所有登录用户访问
          },
          component: HomeView,
        },
        {
          path: 'user',
          name: 'user',
          meta: {
            name: '用户管理',
            roles: ['admin'] // 只有管理员可以访问
          },
          component: User,
        },
        {
          path: 'apply',
          name: 'apply',
          meta: {
            name: '申请处理',
            roles: ['admin'] // 只有管理员可以访问
          },
          component: Apply,
        },
        {
          path: 'chat',
          name: 'chat',
          meta: {
            name: 'Ai',
            roles: ['admin'] // 只有管理员可以访问
          },
          component: Chat,
        },
        {
          path: 'person',
          name: 'person',
          meta: {
            name: '个人信息修改',
            roles: ['admin','customer','merchant'] // 只有管理员可以访问
          },
          component: Person,
        },
        {
          path: 'password',
          name: 'password',
          meta: {
            name: '个人信息修改',
            roles: ['admin','customer','merchant'] // 只有管理员可以访问

          },
          component: Password,
        },
        {
          path: 'operation',
          name: 'operation',
          meta: {
            name: '操作日志',
            roles: ['admin'] // 只有管理员可以访问
          },
          component: Operation,
        },
        {
          path: 'data',
          name: 'data',
          meta: {
            name: '药材管理',
            roles: ['admin','merchant'] // 只有管理员可以访问
          },
          component: Medicine,
        },
        {
          path: 'category',
          name: 'category',
          meta: {
            name: '品类管理',
            roles: ['admin','merchant'] // 只有管理员可以访问
          },
          component: Category,
        },
        {
          path: 'income',
          name: 'income',
          meta: {
            name: '入库管理',
            roles: ['admin'] // 只有管理员可以访问
          },
          component: Income,
        },
        {
          path: 'addShop',
          name: 'addShop',
          meta: {
            name: '药材中心',
            roles: ['admin', 'customer'] // 管理员和客户都可以访问
          },
          component: AddShop,
        },
        {
          path: 'shopCar',
          name: 'shopCar',
          meta: {
            name: '购物车',
            roles: ['admin', 'customer'] // 管理员和客户都可以访问
          },
          component: ShopCar,
        },
        {
          path: 'orderManage',
          name: 'orderManage',
          meta: {
            name: '订单管理',
            roles: ['admin', 'customer'] // 管理员和客户都可以访问
          },
          component: OrderManage,
        },
      ],
    },
    {
      path: '/error404',
      name: 'Error404',
      component: Error404,
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: '/error404',
    },
  ],
});

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('root_token');
  const userRole = localStorage.getItem('user_role');
  const isLoggedIn = !!token;

  // 访问登录页直接放行
  if (to.path === '/login') {
    next();
    return;
  }

  // 未登录用户只能访问登录页
  if (!isLoggedIn) {
    next('/login');
    ElMessage.warning('请先登录！');
    return;
  }

  // 已登录用户访问登录页，重定向到首页
  if (isLoggedIn && to.path === '/login') {
    next('/manager/home'); // 统一重定向到首页
    return;
  }

  // 检查路由权限
  if (to.meta.roles) {
    if (to.meta.roles.includes(userRole)) {
      next();
    } else {
      next('/error404');
      ElMessage.error('您没有权限访问该页面！');
    }
  } else {
    next();
  }
});

export default router;
